<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentMemberView.class.php, $Author:  $ */


/** Component for viewing any member picture
 *
 * @version $Id: PhpFox_ComponentMemberView.class.php 368 2008-02-26 16:04:59Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentMemberView extends BaseComponent
{
    function process()
    {
        $iUserId = $this->getParam('userId');

		if ( !$iUserId )
		{
			return '';
		}

        $oGalleryItem = $this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = $this->_oModule->getTemplate();

        $oSecurityService->checkPerm('gallery', array('read_only', 'full'));
        $oSecurityService->checkAccess('gallery');
        if (!($iItemId = $oReq->getInt('id')))
        {
            App::gotoUrl('public.gallery.main');
        }
        $sUser = $oReq->get('name');
        if (!$sUser)
        {
            App::gotoUrl('public');
        }
        
        if ($iId = $oReq->getInt('makefeature'))
        {
        	if (App::isStaff())
            {
            	$oGalleryItem->load($iId);
            	$oGalleryItem->aData['feature'] = 1;            	
            	$oGalleryItem->update(array('feature'));
            }
            $oUrl->clearParams('makefeature');
            App::forward($oUrl->getUrl());
        }
        if ($iId = $oReq->getInt('removefeature'))
        {
            if (App::isStaff())
            {
	            $oGalleryItem->load($iId);
	            $oGalleryItem->aData['feature'] = '';
	            $oGalleryItem->update(array('feature'));
            }
            $oUrl->clearParams('removefeature');
            App::forward($oUrl->getUrl());
        }              
        
        $aCond = array();
        $aCond['user_id']  = '= "'. $iUserId .'"';
        $aCond['allow'] = '= ""';        
        if (!App::isStaff())
        {
        	if ( $iUserId != App::getUser('id') )
        	{
        		$oSrvFriend = &App::getModuleService('Account', 'Friends');
        		$aCond['who_view'] = ($oSrvFriend->checkIsFriend($sUser, App::getUser('user')) ? '!= "2"' : '= ""');
        	}
        }        
        $aArr = $aCond;
        $aCond['id']    = '= '.$iItemId;
        list($aItems, $iCnt) = $oGalleryItem->getList($aCond, 1, 1);
        $aItem = $aItems[0];
        $aItem['user'] = $sUser;

        if ($iCnt)
        {
            $oGalleryItem->load($aItem['id']);
            $oGalleryItem->aData['view']++;
            $oGalleryItem->update(array('view'));
        }

        if (!$iCnt)
        {
            App::gotoUrl('public');
        }
        $aArr['id'] = '> '.$iItemId;
        list($aItems, $iCnt) = $oGalleryItem->getList($aArr, 1, 1, 'id ASC');
        $iPrevId = 0;
        if ($iCnt)
        {
            $iPrevId = $aItems[0]['id'];
        }
        $aArr = $aCond;
        $aArr['id'] = '< '.$iItemId;
        list($aItems, $iCnt) = $oGalleryItem->getList($aArr, 1, 1, 'id DESC');
        $iNextId = 0;
        if ($iCnt)
        {
            $iNextId = $aItems[0]['id'];
        }
        $sImagePath = App::getParam('gallery.pic.path');
		$sImage = App::getParam('gallery.pic.path').$aItem['id'].'.jpg';
        list($iWidth, $iHeight) = $oGalleryService->getImageResolution($sImage);

		$sActualImage = App::getParam('gallery.pic.url') . $aItem['id'] . '_view.jpg';
		if (!file_exists(App::getParam('gallery.pic.path') . $aItem['id'] . '_view.jpg'))
		{
			$sActualImage = App::getParam('gallery.pic.url') . $aItem['id'] . '.jpg';
		}        
		
		if (App::getParam('bImageSecurity'))
		{
			App::setAccessForContent('gallery', $aItem['id']);
		}
		
        $oTpl->assign(array(
        	'sActualImage' => $sActualImage,
        	'bUseLightbox' => App::getParam('use_lightbox'),
        	'aItem'     => $aItem,
        	'iNextId'   => $iNextId,
        	'iPrevId'   => $iPrevId,
        	'sImageUrl' => App::getParam('gallery.pic.url'),
            'iWidth'           => $iWidth,
            'iHeight'          => $iHeight,
            'sUser' => $sUser
        ));
        return $oTpl->fetch('MemberView.html');
    }
}
?>